const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
const app = express();
app.use(function (req, res, next) {
    //开放权限,允许跨域,设置请求头，允许DELETE方法
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers","token");
    res.header("Access-Control-Allow-Methods","DELETE");
    next();
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//首页
app.get('/getindex', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/index`;
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//关键字搜索
app.get('/getkeyword', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/index/keyword`;
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//关键字搜索结果
app.get('/getkeywordresult', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/product/list?keyword=${req.query.keyword}`;
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//积分专区
app.get('/getpoint', function (req, res) {
    var url = "http://wap.pusai.net/proxy/api/custom";
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//商品分类
app.get('/getcategory', function (req, res) {
    var url = "http://wap.pusai.net/proxy/api/category";
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//分类商品
app.get('/getcategoryproduct', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/product/list?categoryId=${req.query.categoryId}&pageNum=${req.query.pageNum}&sort=${req.query.sort}`;
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//活动
app.get('/getactivity', function (req, res) {
    var url = "http://wap.pusai.net/proxy/api/activity/list";
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//获取活动详情
app.get('/getactivitydetail', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/activity?id=${req.query.id}`;
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//获取活动评论
app.get('/getactivitycomments', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/activity/review?id=${req.query.id}&pageNum=${req.query.pageNum}`;
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//猜你喜欢
app.get('/getlike', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/index/like?pageNum=${req.query.pageNum}`;
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//获取商品详情信息
app.get('/getproductdetail', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/product?id=${req.query.id}`
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//获取商品评论数
app.get('/getproductcommentsnum', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/product/review/number?id=${req.query.id}`
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//获取所有评论
app.get('/getproductallcomments', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/product/review/list?id=${req.query.id}`
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//获取商品好评数
app.get('/getproductgoodcomments', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/product/review/list?id=${req.query.id}&score=${req.query.score}&pageNum=${req.query.pageNum}`
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//获取商品中评数
app.get('/getproductneutralcomments', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/product/review/list?id=${req.query.id}&score=${req.query.score}&pageNum=${req.query.pageNum}`
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//获取商品差评数
app.get('/getproductbadcomments', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/product/review/list?id=${req.query.id}&score=${req.query.score}&pageNum=${req.query.pageNum}`
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//获取主题
app.get('/gettheme', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/theme?id=${req.query.id}`;
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//获取推文
app.get('/gettweet', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/content?id=${req.query.id}`;
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//登录
app.post('/getlogin', function (req, res) {
    console.log(req.body);
    var url = `http://wap.pusai.net/proxy/api/login`;
    request.post(
        {
            url,
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//个人主页
app.get('/getme', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/member/index`;
    request(
        {
            url,
            headers:{
                "token":req.headers.token
            }
    },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//收藏列表
app.get('/getcollection', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/love/list`;
    request({url,headers:{
        "token":req.headers.token
    }},function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//删除收藏商品
app.delete('/delcollection', function (req, res) {
    console.log(req.query.id);
    console.log(req.headers.token);
    var url = `http://wap.pusai.net/proxy/api/love/del?id=${req.query.id}`;
    request.delete(
        {
            url,
            headers:{
                "token":req.headers.token
            },
    },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//获取地址列表
app.get('/getaddresslist', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/address/list`;
    request(
        {
            url,
            headers:{
                "token":req.headers.token
            },
    },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//地址
app.post('/address', function (req, res) {
    console.log(req.body);
    var url = `http://wap.pusai.net/proxy/api/address/save`;
    request.post(
        {
            url,
            headers:{
                "token":req.headers.token
            },
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//获取购物车列表
app.get('/getcarlist', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/cart`;
    request(
        {
            url,
            headers:{
                "token":req.headers.token
            },
    },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//获取订单列表
app.get('/getorderlist', function (req, res) {
    var url = `http://wap.pusai.net/proxy/api/order/list?pageNum=${req.query.pageNum}`;
    request(
        {
            url,
            headers:{
                "token":req.headers.token
            },
    },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//启动服务器
app.listen(3005, function () {
    console.log('start at http://127.0.0.1:3005');
});